const app = getApp()
Component({
    properties: {
        item:{
            type:Object,
        },
        collect:{
            type:Boolean,
            value:false,
        },
        isDelCollectBtn:{
            type:Boolean,
            value:false,
        }
    },
    observers:{

    },
    data: {
        collectedBool:false,
    },



    attached: function() {
        // 在组件实例进入页面节点树时执行
        this.setData({
            collectedBool:this.properties.item.is_collected,
        })
    },
    methods: {
        collectChange(){
            if(this.data.collectedBool){
                this.delCollectBtn()
            }else {
                this.collectBtn()
            }
        },
        // 收藏
        collectBtn: function () {
            let id=this.properties.item.id
            var _this = this;
            if (!app.globalData.token) {
                wx.showModal({
                    title: '提示',
                    content: '您未登录，是否去登陆？',
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '/userPage/pages/login/login',
                            })
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
                return false
            }
            wx.showLoading({
                title: '加载中',
            })
            wx.request({
                url: app.globalData.baseUrl + 'shop/collect',
                data: {
                    shop_id: id,
                    token: app.globalData.token
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success(res) {
                    wx.hideLoading()
                    console.log(res)
                    if (res.data.code == 1000) {
                        wx.showToast({
                            title: '收藏成功',
                            icon: 'success',
                            duration: 2000
                        })
                        _this.setData({
                            collectedBool:true,
                        })
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 2000
                        })
                    }
                }
            })
        },
        // 取消收藏
        delCollectBtn: function () {
            if(!this.properties.isDelCollectBtn){
                return
            }
            let id=this.properties.item.id
            var _this = this;
            wx.showLoading({
                title: '加载中',
            })
            wx.request({
                url: app.globalData.baseUrl + 'shop/cancelCollect',
                data: {
                    shop_id: id,
                    token: app.globalData.token
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success(res) {
                    wx.hideLoading()
                    console.log(res)
                    if (res.data.code == 1000) {
                        wx.showToast({
                            title: '取消成功',
                            icon: 'success',
                            duration: 2000
                        })
                        _this.setData({
                            collectedBool:false,
                        })
                        setTimeout(()=>{
                            _this.triggerEvent('handTalent')
                        },2000)

                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 2000
                        })
                    }
                }
            })
        },
    }
});
